﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class Main_AdverList : System.Web.UI.Page
{
    AdverDao ad = new AdverDao();
    AdverVo av = new AdverVo();
    DataTable dt = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UsersVo uv = new UsersVo();
            uv = (UsersVo)Session["user"];
            if (uv == null)
            {
                Response.Redirect("../index.aspx");
            }
            if (uv.Ugroup == "管理员")
            {
                dt = ad.getAdver();
                gvadver.DataSource = dt;
                gvadver.DataBind();
            }
            else
            {
                Response.Redirect("../index.aspx");
            }
        }
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        bool fileOK = false;
        string path = Server.MapPath("../images/ad/") + fudphoto.FileName;
        if (fudphoto.FileName != "")
        {
            if (fudphoto.HasFile)
            {
                string fileExtension = Path.GetExtension(fudphoto.FileName).ToLower();
                string[] allowerExtensions = { ".gif", ".jpeg" };
                for (int i = 0; i < allowerExtensions.Length; i++)
                {
                    if (fileExtension == allowerExtensions[i])
                    {
                        fileOK = true;
                    }
                }
                fudphoto.PostedFile.SaveAs(path);
                string script = "<script>alert('上传图片成功！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
            else
            {
                string script = "<script>alert('上传失败，格式不允许！');</script>";
                Page.RegisterClientScriptBlock("", script);
            }
        }
        else
        {
            string script = "<script>alert('上传不能为空！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
    }
    protected void gvadver_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string aid = gvadver.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtname = gvadver.Rows[e.RowIndex].FindControl("txtname") as TextBox;
        TextBox txtlink = gvadver.Rows[e.RowIndex].FindControl("txtlink") as TextBox;
        av.Aid = Convert.ToInt32(aid);
        av.Aurl =txtname.Text;
        av.Alink = txtlink.Text;
        Exception ex = new Exception();
        ex = ad.updateAdver(av);
        if (ex == null)
        {
            string script = "<script>window.alert('修改成功！');</script>";
            Page.RegisterClientScriptBlock("", script);
        }
        dt = ad.getAdver();
        gvadver.DataSource = dt;
        gvadver.DataBind();
    }
}
